﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Facade.Biz;

namespace Rison.Web
{
    public partial class _Default : PageBase
    {

        protected void Page_Init(object sender, EventArgs e)
        {

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //测试时用，默认一个登录的用户（不需要后台了）
            //CurrentLoginedUser.SetCurrentUserId("admin", "111111");

            if (!this.IsPostBack)
            {
                InitData();

                BindInfoNews();

                BindCurrentOnlineSurvey();

                //BindTempSource();//正式发布 或 与第三方数据对接后，取消该方法。
            }
        }


        private void InitData()
        {
            DataSourceHelperFacade facade = new DataSourceHelperFacade(InfoCategory.我有诉求);

            DataSet data = facade.GetDepartments();
            this.drpDepartment.DataSource = data.Tables[0].DefaultView;//部门
            this.drpDepartment.DataValueField = "bmid";
            this.drpDepartment.DataTextField = "bmmc";
            this.drpDepartment.DataBind();
            this.drpDepartment.Items.Insert(0, new ListItem("请选择相关部门", "请选择相关部门"));

            //data = facade.GetSQCategory();
            //this.drpSQLB.DataSource = data.Tables[0].DefaultView;//诉求类别
            //this.drpSQLB.DataValueField = "SQLB";
            //this.drpSQLB.DataTextField = "SQLB";
            //this.drpSQLB.DataBind();
            this.drpSQLB.Items.Clear();
            if (drpSQLB.Items.FindByText("投诉") == null)
                this.drpSQLB.Items.Add(new ListItem("投诉", "投诉"));
            if (drpSQLB.Items.FindByText("意见建议") == null)
                this.drpSQLB.Items.Add(new ListItem("意见建议", "意见建议"));
            if (drpSQLB.Items.FindByText("咨询") == null)
                this.drpSQLB.Items.Add(new ListItem("咨询", "咨询"));
            this.drpSQLB.Items.Insert(0, new ListItem("请选择类别", "请选择类别"));

            data.Dispose(); data = null; facade = null;

            //友情连接banding
            //this.drpYQLJ.Items.Clear();
            //drpYQLJ.Items.Add(new ListItem("新浪","http://www.sina.com.cn"));
            //drpYQLJ.Items.Add(new ListItem("新浪", "http://www.sina.com.cn"));
            //this.drpYQLJ.Items.Insert(0, new ListItem("请选择友情链接", "请选择友情链接"));
            

        }


        #region "   绑定公告、纠风动态等发布类新闻   "
        private void BindInfoNews()
        {
            InfosReaderFacade facade = new InfosReaderFacade("gg");//公告
            BindData(gvGongGao, facade.ReadNewsInfos(6, 15, false)); facade = null;

            facade = new InfosReaderFacade("jfdt");//纠风动态
            BindData(gvJFDT, facade.ReadNewsInfos(6, 21, false)); facade = null;

            facade = new InfosReaderFacade("jfhc");//纠风荟萃
            BindData(gvJFFC, facade.ReadNewsInfos(6, 15, false)); facade = null;

            //facade = new InfosReaderFacade("zxzl");//专项治理
            //DataSet data = facade.ReadNewsInfos(1, 200, true); facade = null;
            //SetContent(data, labZXZL);

            //facade = new InfosReaderFacade("zspy");//站所评议
            //data = facade.ReadNewsInfos(1, 200, true); facade = null;
            //SetContent(data, labZSPY);


            //data.Dispose(); data = null;

            //最新诉求
            facade = new InfosReaderFacade("zxsq");
            BindData(gvZXSQ, facade.ReadSuqiuInfos(5, 18)); facade = null;

            //最新回复
            facade = new InfosReaderFacade("zxhf");
            BindData(gvZXHF, facade.ReadSuqiuRepeat(5, 18)); facade = null;

            //回复情况
            facade = new InfosReaderFacade("hfqk");
            BindData_sq(gvHFQK, facade.SuQiuTJ_show(5, 6)); facade = null;



            //DataSourceHelperFacade facade = new DataSourceHelperFacade();

            //BindData(gvGongGao, facade.GetTopNumInfos(InfoCategory.公告, 6));

            //BindData(gvJFDT, facade.GetTopNumInfos(InfoCategory.纠风动态, 6));

            //BindData(gvJFFC, facade.GetTopNumInfos(InfoCategory.纠风荟萃, 6));

            //BindData(gvZXSQ, facade.GetTopNumInfos(InfoCategory.最新诉求, 5));

            //BindData(gvZXHF, facade.GetTopNumInfos(InfoCategory.最新回复, 5));

            //BindData(gvHFQK, facade.GetTopNumInfos(InfoCategory.回复情况, 6));

            //facade = null;

            //访谈实录、正在访谈、预告、政府部门、服务窗口、行业
            //是否是固定的html页，或者也是要读取数据来绑定的？
        }

               private void BindData_sq(GridView gvData, DataSet data)
        {
            if (data != null)
            {
                gvData.DataSource = data.Tables[0].DefaultView;
                gvData.DataBind();

                data.Dispose(); data = null;
            }      
        }

        private void BindData(GridView gvData, DataSet data)
        {
            if (data != null)
            {
                data.Tables[0].Columns.Add("Url", typeof(System.String));
                foreach (DataRow row in data.Tables[0].Rows)
                {
                    row["Url"] = PageGlobalParams.PageUrlBase2 + "/view/InfoBrowseView.aspx?key=" + row["infoId"].ToString();
                }

                //if (gvData.ID == "gvJFDT")
                //{
                //    if (data.Tables[0].Rows.Count > 0)
                //    {
                //        DateTime submittime = System.Convert.ToDateTime(data.Tables[0].Rows[0]["day"]).Date;
                //        foreach (DataRow row in data.Tables[0].Rows)
                //        {
                //            row["ifNew"] = false;
                //            data.Tables[0].Columns.Add("ifNew", typeof(System.Boolean));
                //            if (submittime == System.Convert.ToDateTime(row["day"]).Date)
                //            {
                //                row["ifNew"] = true;
                //            }
                //        }
                //    }
                //}               

                gvData.DataSource = data.Tables[0].DefaultView;
                gvData.DataBind();

                data.Dispose(); data = null;
            }            
        }

        protected void gvJFDT_RowCreated(object sender, GridViewRowEventArgs e)
        {
            try
            {
                e.Row.Cells[2].Visible = false;
                GridView gv = (GridView)sender;
                if (gv.Rows.Count > 0)
                {

                    if (gv.Rows[0].Cells[1].Text != "")
                    {
                        DateTime submittime = System.Convert.ToDateTime(gv.Rows[0].Cells[1].Text).Date;
                        gv.Rows[0].Cells[2].Visible = true;


                        if (e.Row.RowType == DataControlRowType.DataRow && ((DataRowView)e.Row.DataItem).Row.ItemArray[1].ToString().Trim() != "")
                        {
                            if (submittime == System.Convert.ToDateTime(((DataRowView)e.Row.DataItem).Row.ItemArray[1].ToString().Trim()).Date)
                            {
                                e.Row.Cells[2].Visible = true;
                            }
                        }

                    }
                }
            }
            catch
            {
            }
            
        }

         
        private void SetContent(DataSet data, Label lab)
        {
            if (data != null && data.Tables[0].Rows.Count == 1)
            {
                if (!System.DBNull.Value.Equals(data.Tables[0].Rows[0]["ContentNoHTML"]))
                {
                    labZXZL.Text = data.Tables[0].Rows[0]["ContentNoHTML"].ToString();
                }
            }
        }

        #endregion

        #region "   绑定当前在线调查   "
        private void BindCurrentOnlineSurvey()
        {
            DataSourceHelperFacade facade = new DataSourceHelperFacade(InfoCategory.在线调查);
            DataSet data = facade.GetCurrentOnlineSurvey();

            if (data != null && data.Tables[0].Rows.Count > 0)
            {
                labQuestionId.Text = data.Tables[0].Rows[0]["voteid"].ToString();
                labQuestion.Text = data.Tables[0].Rows[0]["votename"].ToString();

                this.chkResultList.DataSource = data.Tables[1].DefaultView;
                this.chkResultList.DataValueField = "answerid";
                this.chkResultList.DataTextField = "content";
                this.chkResultList.DataBind();

                data.Dispose(); data = null;
            }
            else
            {
                this.labQuestion.Text = "你认为我府的政绩好吧？";//测试数据
                this.chkResultList.Items.Add(new ListItem("部门_1", "1"));
                this.chkResultList.Items.Add(new ListItem("部门_2", "2"));
                this.chkResultList.Items.Add(new ListItem("部门_3", "3"));
                this.chkResultList.Items.Add(new ListItem("部门_4", "4"));
            }
        }
        #endregion
          
        protected void btnSearch_SQ_Click(object sender, EventArgs e)
        {
            string dcUrl="";
            if (drpDepartment.SelectedIndex > 0)
            {
                dcUrl = "?depart=" + drpDepartment.SelectedValue;
            }
            if (drpSQLB.SelectedIndex > 0)
            {
                if (dcUrl == "")
                {
                    dcUrl = "?type=" + drpSQLB.SelectedValue;
                }
                else
                {
                    dcUrl =  dcUrl + "&type=" + drpSQLB.SelectedValue;
                }
              }

               //this.Response.Redirect("/wysq/SQListView.aspx" + dcUrl);
               Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>window.open('/wysq/SQListView.aspx'" + dcUrl + ");</script>");
        }

        protected void btnSearch_selfSQ_Click(object sender, EventArgs e)
        {
            if (txtPwd.Text.Trim().Length == 0)
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('请输入密码！');</script>");
            else 
            {
                DataSourceHelperFacade facade = new DataSourceHelperFacade(InfoCategory.我有诉求);
                if (!facade.PasswordExists(txtPwd.Text.Trim()))                
                {
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('对不起，密码输入错误！');</script>");
                }
                else
                {
                    //查看诉求结果.....
                    DataRow dr = facade.GetsuqiuByPwd(txtPwd.Text.Trim());
                    //this.Response.Write("<script language=javascript>window.open('wysq/SQListDetail.aspx?key=" + dr["ID"].ToString() + "','newwindow','width=200,height=200');</script>");
                    //Response.Redirect("wysq/SQListDetail.aspx?key=" + dr["ID"].ToString());
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>window.open('/wysq/SQListDetail.aspx?key=" + dr["ID"].ToString() + "');</script>");
 
                }
            }

        }

        #region "  提交在线调查结果、查看历史记录  "
        protected void ibtnTiJiao_Click(object sender, EventArgs e)
        {
            DataSourceHelperFacade facade = null;
            List<string> answerIds = new List<string>();
            foreach (ListItem item in chkResultList.Items)
            {
                if (item.Selected)
                    answerIds.Add(item.Value);
            }

            try
            {
                if (answerIds.Count == 0)
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('对不起，您未勾选备选列表，不能提交！');</script>");
                else
                {
                    facade = new DataSourceHelperFacade(InfoCategory.在线调查);
                    if (facade.CommitCurrentOnlineSurveyResult(answerIds, Request.UserHostAddress, Request.Browser.Type))
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('提交完成！谢谢您对我们工作的支持！');location.href='default.aspx';</script>");
                        //Response.Redirect("Default.aspx");
                    }
                    else
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('对不起，提交失败！可能是因为网络比较繁忙，请稍后再试...');</script>");
                }
            }
            catch(Exception ex)
            {
                string msg = ex.Message;
            }
            finally
            {
                facade = null; answerIds = null;
            }
        }

        protected void ibtnChaKan_Click(object sender, EventArgs e)
        {
            Response.Redirect("zxdc/OnlineSuveryDetail.aspx?key=" + labQuestionId.Text);
            string qid = labQuestionId.Text;

            //this.Response.Write("<script language=javascript>window.open('zxdc/OnlineSuveryDetail.aspx?key=" + qid + "','newwindow','width=500,height=500');</script>");
            //this.Response.Write("<script language=javascript>window.showModalDialog('zxdc/OnlineSuveryDetail.aspx?key=" + qid + "',,'dialogWidth=200px;dialogHeight=100px');</script>"); 
            //Server.Transfer("zxdc/OnlineSuveryDetail.aspx?key=" + qid);
            //Response.Write("<script>window.showModelessDialog('zxdc/OnlineSuveryDetail.aspx?key=" + qid + "')</script>");
            //Response.Write("<script>window.open('zxdc/OnlineSuveryDetail.aspx?key=" + qid + "','_blank')</script>");

                       

        }
        #endregion

       

        //protected void drpYQLJ_OnSelectedIndexChanged(object sender, EventArgs e)
        //{
        //    Response.Redirect(drpYQLJ.SelectedValue);
        //}


    }
}
